package com.yunkaweilai.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.yunkaweilai.android.R;
import com.yunkaweilai.android.interfaces.ListItemClick;
import com.yunkaweilai.android.model.shop.CompanyTypeModel;
import com.yunkaweilai.android.view.slider.SliderView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/28 0028.
 */

public class SlideAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<CompanyTypeModel.DataBean.ListBean> list;
    private Context context;
    private ListItemClick onClick;

    public ListItemClick getOnClick() {
        return onClick;
    }

    public void setOnClick(ListItemClick onClick) {
        this.onClick = onClick;
    }

    public SlideAdapter(Context context, ArrayList<CompanyTypeModel.DataBean.ListBean> list) {
        super();
        this.list = list;
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CompanyTypeModel.DataBean.ListBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        SliderView slideView = (SliderView) convertView;
        if (slideView == null) {
            View itemView = mInflater.inflate(R.layout.item_list_company, null);
            slideView = new SliderView(context);
            slideView.setContentView(itemView);
            holder = new ViewHolder(slideView);
            slideView.setTag(holder);
        } else {
            holder = (ViewHolder) slideView.getTag();
        }
        slideView.shrink();
        holder.rule.setText(getItem(position).getUnit_name());
        holder.deleteHolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClick.onClick(position);
            }
        });

        return slideView;
    }


    private static class ViewHolder {
        public TextView rule;
        public ViewGroup deleteHolder;

        ViewHolder(View view) {
            rule = (TextView) view.findViewById(R.id.id_tv_company);
            deleteHolder = (ViewGroup) view.findViewById(R.id.holder);
        }
    }
}